<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{{number}}.{{title}}</title>
    <!-- 引入ACE CDN -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.6/ace.js" type="text/javascript"
        charset="utf-8"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.6/ext-language_tools.js" type="text/javascript"
        charset="utf-8"></script>
    <!-- 引入JQuery CDN -->
    <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>

    <style>
        * {
            margin: 0;
            padding: 0;
        }

        html,
        body {
            width: 100%;
            height: 100%;
        }

        .container .navbar {
            width: 100%;
            height: 50px;
            background-color: black;
            /* 父级元素设置overflow避免子元素浮动带来的影响 */
            overflow: hidden;
        }

        .container .navbar a {
            /* 设置为行内块元素 */
            display: inline-block;
            /* 设置宽度 */
            width: 80px;
            /* 设置字体颜色 */
            color: white;
            /* 设置字体大小 */
            font-size: large;
            /* 把文字的高度设置为和导航栏一样高度 */
            line-height: 50px;
            /* 去除超链接下划线 */
            text-decoration: none;
            /* 设置文字居中 */
            text-align: center;
        }

        .container .navbar a:hover {
            background-color: green;
        }

        .container .navbar .login {
            float: right;
        }

        .container .part1 {
            width: 100%;
            height: 600px;
            overflow: hidden;
        }

        .container .part1 .left_desc {
            width: 50%;
            height: 600px;
            float: left;
            overflow: scroll;
        }

        .container .part1 .left_desc h2 {
            padding-top: 10px;
            padding-left: 10px;
        }

        .container .part1 .left_desc pre {
            padding-top: 10px;
            padding-left: 10px;
            font-size: medium;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }

        .container .part1 .right_code {
            width: 50%;
            float: right;
        }

        .container .part1 .right_code .ace_editor {
            height: 600px;
        }

        .container .part2 {
            width: 100%;
            overflow: hidden;
        }

        .container .part2 .result {
            width: 300px;
            float: left;
        }

        .container .part2 .btn-submit {
            width: 80px;
            height: 50px;
            font-size: large;
            float: right;
            background-color: #26bb9c;
            color: #fff;
            border-radius: 5px;
            border: 0px;
            margin-top: 10px;
            margin-right: 10px;
        }

        .container .part2 button:hover {
            background-color: #1cb597;
        }

        .container .part2 .result {
            margin-top: 15px;
            margin-left: 15px;
        }

        .container .part2 .result pre{
            font-size: large;
        }
    </style>
</head>

<body>
    <div class="container">
        <!-- 导航栏，仅作为装饰 -->
        <div class="navbar">
            <a href="/">首页</a>
            <a href="/all_questions">题库</a>
            <a href="#">竞赛</a>
            <a href="#">讨论</a>
            <a href="#">求职</a>
            <a href="#">个人</a>
            <a class="login" href="#">登录</a>
        </div>
        <!-- 左右分别展示题目描述和代码编写框 -->
        <div class="part1">
            <div class="left_desc">
                <h2><span id="number">{{number}}</span>.{{title}}_{{level}}</h2>
                <pre>{{desc}}</pre>
            </div>
            <div class="right_code">
                <pre id="code" class="ace_editor"><textarea class="ace_text-input">{{pre_code}}</textarea></pre>
            </div>
        </div>
        <!-- 提交并展示结果 -->
        <div class="part2">
            <div class="result"></div>
            <button class="btn-submit" onclick="submit()">提交</button>
        </div>
    </div>

    <script>
        //初始化对象
        editor = ace.edit("code");

        //设置⻛格和语⾔（更多⻛格和语⾔，请到github上相应⽬录查看）
        // 主题⼤全：http://www.manongjc.com/detail/25-cfpdrwkkivkikmk.html
        editor.setTheme("ace/theme/monokai");
        editor.session.setMode("ace/mode/c_cpp");

        // 字体⼤⼩
        editor.setFontSize(16);
        // 设置默认制表符的⼤⼩:
        editor.getSession().setTabSize(4);

        // 设置只读（true时只读，⽤于展⽰代码）
        editor.setReadOnly(false);

        // 启⽤提⽰菜单
        ace.require("ace/ext/language_tools");
        editor.setOptions({
            enableBasicAutocompletion: true,
            enableSnippets: true,
            enableLiveAutocompletion: true
        });

        function submit() {
            // 1. 收集当前⻚⾯的有关数据, 1. 题号 2.代码
            let code = editor.getSession().getValue();
            let number = $(".container .part1 .left_desc h2 #number").text(); //题号
            let judge_url = "/judge/" + number; //请求路由

            // 2. 构建json，并通过ajax向后台发起json请求
            $.ajax({
                method: 'Post', //请求方法
                url: judge_url, //请求url
                dataType: 'json', //告知后端想要收到的数据格式
                contentType: 'application/json;charset=utf-8', //请求的数据格式
                data: JSON.stringify({ //发送的数据
                    'code': code,
                    'input': ''
                }),
                success: function (data) {
                    show_result(data);
                }
            });

            // 3. 得到结果，解析并显⽰到 result中
            function show_result(data) {
                let result_div = $(".container .part2 .result");
                result_div.empty(); //清空上一次的运行结果

                let _status = data.status;
                let _reason = data.reason;

                let reason_lable = $("<p>", {
                    text: _reason
                });
                reason_lable.appendTo(result_div);

                if (_status == 0) //请求成功，但不代表程序正确
                {
                    let _stdout = data.stdout;
                    let _stderr = data.stderr;

                    let stdout_lable = $("<pre>", {
                        text: _stdout
                    });

                    let stderr_lable = $("<pre>", {
                        text: _stderr
                    });

                    stdout_lable.appendTo(result_div);
                    stderr_lable.appendTo(result_div);
                }
                else //请求失败
                {
                    //do nothing
                }
            }
        }
    </script>
</body>

</html>